package ogmios.components;
import javax.swing.*;
import java.awt.*;
import javax.swing.text.*;
import javax.swing.event.*;
import javax.swing.plaf.*;
import ogmios.core.*;
public class OgmiosTextPane extends JTextPane
{

public OgmiosTextPane()
{
addCaretListener(
	new CaretListener()
	{
	public void caretUpdate(final CaretEvent e)
	{
	SwingUtilities.invokeLater(
	new Runnable()
	{
	public void run()
	{
	try
	{
	java.awt.Rectangle r = modelToView(getCaretPosition());
	r.x+=2;
	scrollRectToVisible(r);
	}
	catch(Throwable ex)
	{
	OgmiosUtilities.showErrorBox(null, ex, "Code:Caret Error");
	}
	}
	}
	);
	}
	}
	);

//setEditorKitForContentType("text/java",new StyledEditorKit());
}
public Document createDefaultDocument()
{
Document document = getEditorKit().createDefaultDocument();
document.putProperty(DefaultEditorKit.EndOfLineStringProperty, System.getProperty("line.separator"));
return document;
}
public String getText()
{
try
{
Document doc= getDocument();
return doc.getText(0, doc.getLength());
}
catch(BadLocationException ble)
{
return "";
}
}
	public boolean getScrollableTracksViewportWidth()
	{
	Component parent = getParent();
  	ComponentUI ui = getUI();
  	
	return parent != null ? (ui.getPreferredSize(this).width < parent.getSize().width) : true;
	}


public boolean print(final boolean selectionOnly)
{
java.awt.print.PrinterJob printer = java.awt.print.PrinterJob.getPrinterJob();
if (printer.printDialog())
{
try
{
printer.setPrintable(
	new java.awt.print.Printable()
	{
	JTextPane copy; 
	{
	copy = new JTextPane();

	copy.setEditorKit(OgmiosTextPane.this.getEditorKit());
	if (selectionOnly)
	{
	Document document = createDefaultDocument();
	try
	{
	document.insertString(0, getSelectedText(), null);
	}
	catch(Throwable ex)
	{
	}
	copy.setDocument(document);
	}
	else
	{
	copy.setDocument(OgmiosTextPane.this.getDocument());
	}
	copy.setFont(OgmiosTextPane.this.getFont());
	copy.setForeground(OgmiosTextPane.this.getForeground());
	}
	int currentPage = -1;
	double pageStartY= 0;
	double pageEndY =  0;
	public int print(Graphics graphics, java.awt.print.PageFormat pageFormat, int index)
	{
	double scale = 1.0;	
	Graphics2D g = (Graphics2D)graphics;
	View rootView;
	copy.setSize((int)pageFormat.getImageableWidth(), Integer.MAX_VALUE);
	copy.validate();
	rootView = copy.getUI().getRootView(copy);
	if (copy.getMinimumSize().getWidth() > pageFormat.getImageableWidth())
	{
	scale = pageFormat.getImageableWidth()/copy.getMinimumSize().getWidth();
	g.scale(scale, scale);
	}
	g.setClip((int) (pageFormat.getImageableX()/scale),
	(int)(pageFormat.getImageableY()/scale),
	(int)(pageFormat.getImageableWidth()/scale),
	(int)(pageFormat.getImageableHeight()/scale));
	if (index	 > currentPage)
	{
	currentPage = index;
	pageStartY +=pageEndY;
	pageEndY = g.getClipBounds().getHeight();
	}
	g.translate(g.getClipBounds().getX(), g.getClipBounds().getY());
	Rectangle allocation = new Rectangle(0, 
		(int)-pageStartY,
		(int)(copy.getMinimumSize().getWidth()),
		(int)(copy.getPreferredSize().getHeight()));
	if (printView(g, allocation, rootView))
		return java.awt.print.Printable.PAGE_EXISTS;
	else
	{
	pageStartY=0;
	pageEndY=0;
	currentPage = -1;
	return java.awt.print.Printable.NO_SUCH_PAGE;
	}
	}
	private boolean printView(Graphics2D g, Shape allocation, View view)
	{
	boolean exists = false;
	Rectangle clip = g.getClipBounds();
	Shape childAllocation;
	View childView;
	if (view.getViewCount() > 0)
	{
		for (int i=0; i<view.getViewCount();i++)
		{
			childAllocation = view.getChildAllocation(i, allocation);
			if (childAllocation != null)
			{
				childView = view.getView(i);
				if (printView(g, childAllocation, childView))
				{
				exists = true;
				}
			}
		}
	}
	else
	{
	if (allocation.getBounds().getMaxY() >= clip.getY())
	{
		exists = true;
		if ((allocation.getBounds().getHeight() > clip.getHeight()) && (allocation.intersects(clip)))
		{
			view.paint(g, allocation);
		}
		else
		{
			if (allocation.getBounds().getY() >= clip.getY())
			{
				if (allocation.getBounds().getMaxY()<= clip.getMaxY())
				{
					view.paint(g, allocation);
				}
				else
				{
					if (allocation.getBounds().getY() < pageEndY)
					{
						pageEndY = allocation.getBounds().getY();
					}
				}
			}
		}
	}
	}
	return exists;
	}
	}, new java.awt.print.PageFormat());

printer.print();
return true;
}
catch(java.awt.print.PrinterException printerException)
{
printerException.printStackTrace();
}


}


return false;

}


	
}
